//
//  EAppRankingTableViewCell.m
//  AppRanking
//
//  Created by Gang.Wang on 13-1-15.
//  Copyright (c) 2013年 Gang.Wang. All rights reserved.
//

#import "EAppRankingTableViewCell.h"

#import <UIKit/UIKit.h>
#import "MIMLineGraph.h"

@interface EAppRankingTableViewCell()<LineGraphDelegate>

@property (nonatomic, strong) MIMLineGraph * mLineGraph;
@property (nonatomic, strong) NSArray * yValuesArray;

@property (nonatomic, strong) NSDictionary * verticalLinesProperties;
@property (nonatomic, strong) NSDictionary * horizontalLinesProperties;

@property (nonatomic, strong) NSArray * xValuesArray;
@property (nonatomic, strong) NSArray * xTitlesArray;

@property (nonatomic, strong)     NSArray *anchorPropertiesArray;

@end


@implementation EAppRankingTableViewCell

- (void) setAppRanking:(ERanking *)appRanking
{
    YRelease(_appRanking)
    _appRanking = [appRanking retain];
    
    [self showDataToView];
}


- (void) showDataToView
{
    [self.mLineGraph removeFromSuperview];
    YRelease(self.mLineGraph)
    
    MIMLineGraph * temp_LineGraph = [[MIMLineGraph alloc] init];
    
    [temp_LineGraph setFrame:CGRectMake(-20, 0, self.width + 20, self.height)];
    [temp_LineGraph setDelegate:self];
    
    self.mLineGraph = temp_LineGraph;
    YRelease(temp_LineGraph);
    
    self.horizontalLinesProperties=nil;
    self.anchorPropertiesArray=nil;
    
    NSMutableArray * xArrays = [[NSMutableArray alloc] initWithCapacity:[self.appRanking.rankingValues count]];
    NSMutableArray * yAppRankingArray = [[NSMutableArray alloc] initWithCapacity:[self.appRanking.rankingValues count]];
    
    int i = 0;
    for (ERankingPoint * point in self.appRanking.rankingValues)
    {
        if (!point)
        {
            continue;
        }
        
        if (self.indexPath.section > 3)
        {
            [xArrays addObject:[NSString stringWithFormat:@"%d点", i++]];
            [yAppRankingArray addObject: NUMBER(point.yValue)];
        }
        else
        {
            [xArrays addObject:point.xValue];
            [yAppRankingArray addObject: NUMBER(point.yValue)];
        }
        
    }
    self.xTitlesArray = xArrays;
    self.xValuesArray = xArrays;
    
    //    int addtionValue = [[yAppRankingArray lastObject] intValue];
    
//    [yAppRankingArray addObject:NUMBER(0)];
    
    self.yValuesArray = [NSArray arrayWithObjects:yAppRankingArray, nil];
    
    YRelease(xArrays);
    YRelease(yAppRankingArray);
    
    
    self.mLineGraph.delegate=self;
    self.mLineGraph.xTitleStyle=X_TITLES_STYLE2;
    [self.mLineGraph.titleLabel setText:self.appRanking.subTitle];
    self.mLineGraph.topMargin = 5;
    self.mLineGraph.bottomMargin=30;
    self.mLineGraph.rightMargin=5;
    self.mLineGraph.leftMargin=0;
    
    [self addSubview:self.mLineGraph];
    
    [self.mLineGraph drawMIMLineGraph];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



#pragma mark - LineGraphDelegate
-(NSArray *)valuesForGraph:(id)graph
{
    return self.yValuesArray;
}

-(NSArray *)valuesForXAxis:(id)graph
{
    return self.xValuesArray;
}

-(NSArray *)titlesForXAxis:(id)graph
{
    
    return self.xTitlesArray;
    
}

-(NSArray *)AnchorProperties:(id)graph
{
    return self.anchorPropertiesArray;
}

-(NSDictionary *)horizontalLinesProperties:(id)graph
{
    return self.horizontalLinesProperties;
    
}

-(NSDictionary*)verticalLinesProperties:(id)graph
{
    return self.verticalLinesProperties;
}


@end
